<? 
ob_start();
session_start();
$sess_id=$_SESSION['sess_id'];
$sess_user=$_SESSION['sess_user'];
$sess_status=$_SESSION['sess_status'];
$flag = $_SESSION['sess_flag'];
if ($sess_id<>session_id() || $sess_user=="" || $sess_status<>"active") {
	$_SESSION['cur_page'] = "admin/index.php?module=fault_type";
	header( "Location: ../login.php");exit();
}
?>
<body onLoad="javascript:showFault_type();">
<form name='afault' method='post' action=''>
<?php
include('../config.inc.php');
echo "<meta http-equiv='content-type' content='text/html; charset=$charset'>"; 
?>
<div id='faultbody'></div>
<div id='formaddfault' style="padding:5px 0px 5px 16px; "></div>
<input name='addf' type='button' id='addf' value='เพิ่ม' onClick='javascript:genformadd();'>
<div id='faultstatus'></div>

</form>
</body>
<script language='javascript' src='../js/script.js'></script>
<script language="javascript">
function GetXmlHttp(){
	var HttPRequest = false;
	if(window.XMLHttpRequest) { // Mozilla, Safari,...
		HttPRequest = new XMLHttpRequest();
		if (HttPRequest.overrideMimeType) {
			HttPRequest.overrideMimeType('text/html');
		}
		return HttPRequest;
	}else if(window.ActiveXObject) { // IE
		try {
			HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
			 return HttPRequest;
	} 	  
	if (!HttPRequest) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
}
function showFault_type(){
	var page = "showfault_type";
	var url = 'updatesql.php';
	var pmeters = "page="+page;
	var xmlHttp = GetXmlHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 3){document.getElementById("faultbody").innerHTML = "Now is Loading...";}
		if(xmlHttp.readyState == 4){
			document.getElementById("faultbody").innerHTML = xmlHttp.responseText;
		}		
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", pmeters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);

	
}
function dataFault(type,fname,fdetail,page){
	//alert(type);
	//alert(fname);
	//alert(fdetail);
	//alert(page);
	var url = 'updatesql.php';
	var pmeters = "type="+type+"&fname="+fname+"&fdetail="+fdetail+"&page="+page;
	var xmlHttp = GetXmlHttp();
	
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 3){ document.getElementById("faultstatus").innerHTML = "Loading...";}
		if(xmlHttp.readyState == 4){ 
			document.getElementById("faultstatus").innerHTML = xmlHttp.responseText;
			}
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", pmeters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
}
function delFault(type,page){
	//alert(type);
	//alert(page);
	var url = 'updatesql.php';
	var pmeters = "type="+type+"&page="+page;
	var xmlHttp = GetXmlHttp();
	
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 3){ document.getElementById("faultstatus").innerHTML = "Loading...";}
		if(xmlHttp.readyState == 4){ 
			document.getElementById("faultstatus").innerHTML = xmlHttp.responseText;
			showFault_type();
			}
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
}
function genformadd(){
	var id = "";
	var page = "getnumrow";
	var url = 'updatesql.php';
	var pmeters = "page="+page;
	var xmlHttp = GetXmlHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 3){document.getElementById("formaddfault").innerHTML = "Now is Loading...";}
		if(xmlHttp.readyState == 4){
			id = xmlHttp.responseText;
			id = ++id;
			document.getElementById("formaddfault").innerHTML = "<input name='afid' type='text' id='afid' size='2' value='"+id+"'> <input name='afname' type='text' id='afname' size='10'> <input name='afdetail' type='text' id='afdetail' size='10'> <input name='addf' type='button' id='addf' value='บันทึกข้อมูล' onClick=\"javascript:addFault_Type(document.afault.afid.value,document.afault.afname.value,document.afault.afdetail.value,'addfault');\">";
		}		
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//xmlHttp.setRequestHeader("Content-length", pmeters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
}
function addFault_Type(type,fname,fdetail,page){
	//alert(type);
	//alert(fname);
	//alert(fdetail);
	//alert(page);
	var url = 'updatesql.php';
	var pmeters = "type="+type+"&fname="+fname+"&fdetail="+fdetail+"&page="+page;
	var xmlHttp = GetXmlHttp();
	
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 3){ document.getElementById("faultstatus").innerHTML = "Loading...";}
		if(xmlHttp.readyState == 4){ 
			document.getElementById("formaddfault").innerHTML = "";
			showFault_type();
			document.getElementById("faultstatus").innerHTML = xmlHttp.responseText;
			}
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", pmeters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
}
</script>
